package com.hyh.ai.controller;

import com.hyh.ai.services.ToolsService;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.PromptChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.client.advisor.vectorstore.QuestionAnswerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.deepseek.DeepSeekChatModel;
import org.springframework.ai.tool.ToolCallbackProvider;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.time.LocalDate;
import java.time.LocalDateTime;


/**
 * @author xushu
 * @version 1.0
 * @description:
 */
@RestController
@CrossOrigin
public class OpenAiController  {

    private final ChatClient chatClient;
    private final VectorStore vectorStore;

    public OpenAiController(DeepSeekChatModel deepSeekChatModel,
                            ChatMemory chatMemory,
                            ToolsService toolsService,
                            ToolCallbackProvider toolCallbackProvider,
                            VectorStore vectorStore) {
        this.chatClient = ChatClient.builder(deepSeekChatModel)
                .defaultSystem("""
                        ## 你是“古月”航空公司的客户聊天支持代理。请以友好、乐于助人且愉快的方式来回复。
                        你正在通过在线聊天系统与客户互动
                        ## 要求
                        1.在涉及增删改（除了查询）function-call前，必须等待用户回复“确认”一类的词后再调用tool
                        2.请讲中文
                        
                        今天的日期是{current_time}.
                        """)
                .defaultAdvisors(PromptChatMemoryAdvisor.builder(chatMemory).build()
                                 ,new SimpleLoggerAdvisor())
                .defaultTools(toolsService)
                .defaultToolCallbacks(toolCallbackProvider)
                .build();
        this.vectorStore = vectorStore;
    }


    @CrossOrigin
    @GetMapping(value = "/ai/generateStreamAsString", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> generateStreamAsString(@RequestParam(value = "message", defaultValue = "讲个笑话") String message) {

        // 发送请求
        return chatClient.prompt()
                .user(message)
                .system(p -> p.param("current_time", LocalDate.now()))
                .advisors(QuestionAnswerAdvisor.builder(vectorStore)
                        .searchRequest(SearchRequest.builder()
                                .topK(5)
                                .similarityThreshold(0.3)
                                .build())
                        .build())
                .stream()
                .content()
                .concatWith(Flux.just("[complete]")); // 在流末尾推送 complete;
    }



}
